using System;
using System.Collections.Generic;
using System.Text;
using Ltf.Remoting.Settings;
using Lizard.Remoting.Settings;
using Microsoft.TeamFoundation.VersionControl.Client;
using Ltf.Remoting.TFAccess;

namespace Ltf.Remoting.TeamFoundation
{
	public class ServerConnections
	{
//		public static Dictionary<string, TFSettings> ServerSettings;

		private static Dictionary<string, TFServerConnection> serverConnections;
		
		
		static ServerConnections()
		{
//			ServerSettings = new Dictionary<string, TFSettings>();
			serverConnections = new Dictionary<string, TFServerConnection>();
		}

		public static void AddConnection(TFServerConnection connection)
		{
			serverConnections.Add(connection.Settings.Server, connection);
		}

		private static TFServerConnection defaultConnection;

		private static TFServerConnection Connection
		{
			get { return defaultConnection; }
			set { defaultConnection = value; }
		}

		public static TFServerConnection DefaultConnection
		{
			get { return defaultConnection; }

			//TODO: get rid of this if not used
			//set {
			//  defaultConnection = value;
			//  serverConnections.Clear();
			//  ServerSettings.Clear();
			//  ServerSettings.Add(defaultConnection.Settings.Server, defaultConnection.Settings);
			//  foreach (TFSecondarySettings ss in defaultConnection.Settings.AlternativeServerSettings)
			//  {
			//    ss.PrimarySettings = defaultConnection.Settings;
			//    TFServerConnection connection = new TFServerConnection(ss);
			//    serverConnections.Add(connection.Settings.Server, connection);
			//    ServerSettings.Add(connection.Settings.Server, connection.Settings);
			//  }
			//}
		}

		public static TFServerConnection GetConnectionForServerPath(string serverPath, Workspace workspace)
		{
			string serverPathName;
			int v;
			LizardFileAccess.GetNameAndVersionFromPath(serverPath, out serverPathName, out v);

			string path = workspace.GetWorkingFolderForServerItem(serverPath).LocalItem;

			foreach (TFServerConnection connection in serverConnections.Values)
			{
				if (connection.Settings is TFSecondarySettings)
					if (((TFSecondarySettings)connection.Settings).AppliesToPath(path))
						return connection;
			}
			return defaultConnection;
		}

		
		public static TFServerConnection GetConnectionForPath(string path)
		{
			foreach (TFServerConnection connection in serverConnections.Values)
			{
				if (connection.Settings is TFSecondarySettings)
					if (((TFSecondarySettings)connection.Settings).AppliesToPath(path))
						return connection;
			}
			return defaultConnection;
		}

		public static void CreateConnectionsFromSettings(TFSettings settings)
		{
			serverConnections.Clear();
			defaultConnection = new TFServerConnection(settings);
			foreach (TFSecondarySettings secondarySettings in settings.AlternativeServerSettings)
			{
				TFServerConnection altConnection = new TFServerConnection(secondarySettings);
				serverConnections.Add(secondarySettings.Server, altConnection);
			}
		}



		public static TFServerConnection GetConnectionByName(string name)
		{
			if (DefaultConnection.Settings.Server == name)
				return DefaultConnection;

			return serverConnections[name];
		}
	}
}
